package com.qd.panda.stroke;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.repeat.annotation.RepeatSubmit;
import com.cdqidi.util.page.PageUtil;
import com.qd.common.panda.domain.entity.carpark.GasCalculatePriceDTO;
import com.qd.common.panda.domain.entity.carpark.calculate.CalculatePriceDTO;
import com.qd.common.panda.domain.entity.order.GasCalculatePriceOrderDTO;
import com.qd.common.panda.domain.entity.order.OrderGasDTO;
import com.qd.common.panda.domain.entity.order.OrderQueryDTO;
import com.qd.common.panda.domain.entity.order.TotalPreferentialAmountDTO;
import com.qd.common.panda.domain.entity.stroke.PayParamDTO;
import com.qd.panda.service.park.GasStationHolderService;
import com.qd.panda.service.stroke.DisplayUtil;
import com.qd.panda.service.stroke.UserStrokeHolderService;
import com.qd.panda.service.stroke.order.UserStrokeOrderGasService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * APP_加油站
 *
 * @author sjk
 */
@RestController
@RequiredArgsConstructor
@Validated
@RequestMapping("/app/gasStation/")
@Slf4j
public class AppGasStationController {

    private final GasStationHolderService gasStationHolderService;
    private final UserStrokeOrderGasService userStrokeOrderGasService;
    private final UserStrokeHolderService userStrokeHolderService;
    /**
     * 根据输入的价格计算折后价格
     *
     * @param calculatePriceDTO 请求参数
     * @return 价格
     */
    @GetMapping("/calculatePrice")
    public ApiResultWrapper<Object> calculatePrice(@Validated GasCalculatePriceDTO calculatePriceDTO) {
        final CalculatePriceDTO priceDTO = gasStationHolderService.calculatePrice(calculatePriceDTO);
        calculatePriceDTO.freeData();
        Optional.ofNullable(priceDTO.getDetailList()).ifPresent(List::clear);
        return ApiResult.success(priceDTO);
    }

    /**
     * 创建加油订单
     *
     * @param gasCalculatePriceOrderDTO 参数
     * @return 支付参数
     */
    @RepeatSubmit
    @PostMapping("/createGasOrder")
    public PayParamDTO createGasOrder(@Validated @RequestBody GasCalculatePriceOrderDTO gasCalculatePriceOrderDTO) {
        return gasStationHolderService.createOrder(gasCalculatePriceOrderDTO);
    }

    /**
     * 查询加油记录列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/orderGasPage")
    public IPage<OrderGasDTO> orderGasPage(OrderQueryDTO dto) {
        return userStrokeOrderGasService.orderGasPage(PageUtil.buildPage(dto), dto);
    }

    /**
     * 优惠总金额
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/totalPreferentialAmount")
    public TotalPreferentialAmountDTO totalPreferentialAmount(OrderQueryDTO dto) {
        return userStrokeOrderGasService.totalPreferentialAmount(dto);
    }

    /**
     * 加油记录详情
     *
     * @param orderId 订单ID不能为空
     * @return 详情
     */
    @GetMapping(value = "/getOrderGasByOrderId")
    public OrderGasDTO getOrderGasByOrderId(@NotBlank(message = "订单ID不能为空") String orderId) {
        return userStrokeOrderGasService.getOrderGasByOrderId(orderId);
    }

    /**
     * 支付失败
     *
     * @param map 订单ID
     * @return true or false
     */
    @RepeatSubmit
    @PostMapping("/payError")
    public ApiResultWrapper<Object> payError(@Validated @RequestBody Map<String, String> map) {
        String bid = map.get("orderId");
        if (!StringUtils.hasLength(bid)) {
            return ApiResult.error("订单ID不能为空");
        }
        userStrokeHolderService.afterPay(bid, DisplayUtil.PAY_STATUS_2, DisplayUtil.ORDER_TYPE_4);
        return ApiResult.success();
    }
}